package day_2022_7_to_8.day48;

/**
 * @author haomin
 * @date 2022/08/10 10:28
 **/
public class Day25_1_Num640_SolveEquation {
    class Solution {
        public String solveEquation(String s) {
            int x = 0, num = 0, n = s.length();
            char[] cs = s.toCharArray();
            for (int i = 0, op = 1; i < n; ) {
                if (cs[i] == '+') {
                    op = 1; i++;
                } else if (cs[i] == '-') {
                    op = -1; i++;
                } else if (cs[i] == '=') {
                    x *= -1; num *= -1; op = 1; i++;
                } else {
                    int j = i;
                    while (j < n && cs[j] != '+' && cs[j] != '-' && cs[j] != '=') j++;
                    if (cs[j - 1] == 'x') x += (i < j - 1 ? Integer.parseInt(s.substring(i, j - 1)) : 1) * op;
                    else num += Integer.parseInt(s.substring(i, j)) * op;
                    i = j;
                }
            }
            if (x == 0) return num == 0 ? "Infinite solutions" : "No solution";
            else return "x=" + (num / -x);
        }
    }
}